<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use EasyWeChat\Factory;
use think\facade\Db;
use think\Request;

class Business extends Api
{
    protected $with = ['province','city','area','lb'];
    //商户端-静默授权-获取openid
    public function jm_login()
    {
        $code = input('code');
        $id = input('id');
        if(empty($code)) return msgerror('参数缺失');
        $app = Factory::miniProgram($this->sh_easy_config);
        $data = $app->auth->session((string)$code);
        $this->model->where('id',$id)->update(['openid'=>$data['openid']]);
        return datasuccess($data);
    }
    //用户端-默认门店
    public function mr_read()
    {
        $data = $this->model->where('status',1)->with($this->with)->find()->toArray();
        return datasuccess($data);
    }
    //用户端-门店列表-切换门店
    public function qh_index()
    {
        $post = param();
        if(!isset($post['x']) || !isset($post['y'])){
            return msgerror('参数缺失');
        }
        $lng = $post['x'];
        $lat = $post['y'];
        $page = $post['page'] ?? 1;
        $limit = $post['limit'] ?? 20;
        $qswz = (($page-1)*$limit);
        $data = Db::query("select *, ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$lat}*PI()/180-y*PI()/180)/2),2)+COS({$lat}*PI()/180)*COS(y*PI()/180)*POW(SIN(({$lng}*PI()/180-x*PI()/180)/2),2)))*1000) AS juli from ysh_business order by `juli` asc limit {$qswz},{$limit}");
        foreach ($data as $k=>$v)
        {
            $res = app('app\api\model\Lb')->where('business_id',$v['id'])->select();
            if(!empty($res) && !empty($res->toArray())){
                $data[$k]['lb'] = $res->toArray();
            }else{
                $data[$k]['lb'] = [];
            }
        }
        return datasuccess($data);
    }
    //用户端-门店锁场信息
    public function sc()
    {
        // $this->checkLogin();
        $post = param();
        if(!isset($post['id']) || !isset($post['date'])) return msgerror('参数缺失');
        $BusinessStatus_model = app('app\api\model\BusinessStatus');
        $data = $BusinessStatus_model->where([
            ['business_id','=',$post['id']],
            ['date','=',$post['date']]
        ])->order('type asc')->select();
        return datasuccess($data);
    }
    //商户登录
    public function login()
    {
        $post = input('post.');
        if(!isset($post['username']) || !isset($post['password']) || empty($post['username']) || empty($post['password'])){
            return msgerror('账号或密码不能空');
        }
        //密码加密验证
        $post['password'] = jm($post['password']);
        $data = $this->model->where(['username'=>$post['username'],'password'=>$post['password']])->with($this->with)->find();
        if(!empty($data) && !empty($data->toArray()))
        {
            $data = $data->toArray();
            $token = random(32);
            $this->model::update(['id'=>$data['id'],'token'=>$token]);
            $data['token'] = $token;
            return datasuccess($data,'登录成功');
        }else{
            return msgerror('账号或密码错误');
        }
    }
    //商户端-获取最新商户数据
    public function read()
    {
        $this->businessCheckLogin();
        urladd($this->businessinfo);
        return datasuccess($this->businessinfo);
    }

    //商户端-数据修改
    public function upload()
    {
        $this->businessCheckLogin();
        $post = array_filter(input('post.'));
        if(!empty($post)){
            $post['id'] = $this->businessinfo['id'];
            unset($post['password']);
            unset($post['username']);
            $this->model::update($post);
            return datasuccess();
        }else{
            return msgerror('缺少参数');
        }
    }
    //添加订阅次数
    public function dy()
    {
        $this->businessCheckLogin();
        $this->model->where('id',$this->businessinfo['id'])->inc('dy_num')->update();
        return datasuccess([],'添加成功');
    }

}